<?php

namespace Siese\LoginBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;

/**
 * Usuarios
 *
 * @ORM\Table(name="usuarios")
 * @ORM\Entity
 */
class Usuarios implements AdvancedUserInterface, \Serializable
{
   /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=25, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=64)
     */
    private $password;


    /**
     * @ORM\Column(name="fe_ingreso", type="datetime", length=64)
     */
    private $feIngreso;
    
    /**
     * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
     *
     */
    private $roles;    

    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;
    
    /**
     * @ORM\Column(type="string", length=32)
     */
    private $salt;    
    
   /**
     * @ORM\OneToMany(targetEntity="\Siese\EntityBundle\Entity\UsuariosSedes", mappedBy="usuario")
     */
    private $sedesUsuario;   
    
    /**
     * @var \Siese\EntityBundle\Entity\Funcionarios
     *
     * @ORM\OneToOne(targetEntity="\Siese\EntityBundle\Entity\Funcionarios", mappedBy="usuario")
     */
    private $funcionario;   
    

    public function __construct()
    {
        $this->isActive = true;
        $this->roles = new ArrayCollection();
        // may not be needed, see section on salt below
         $this->salt = md5(uniqid(null, true));
    }

    /**
     * @inheritDoc
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return $this->salt;
    }
    
    /**
     * Set salt
     *
     * @param string $salt
     * @return Users
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }    

    /**
     * @inheritDoc
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * @inheritDoc
     */
    public function getRoles()
    {
      //return array('ROLE_USER');
       return $this->roles->toArray();
    }

    /**
     * @inheritDoc
     */
    public function eraseCredentials()
    {
    }

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
        ) = unserialize($serialized);
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     * @return Usuarios
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return Usuarios
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return Usuarios
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return $this->isActive;
    }

    /**
     * Add roles
     *
     * @param \Siese\LoginBundle\Entity\Role $roles
     * @return Usuarios
     */
    public function addRole(\Siese\LoginBundle\Entity\Role $roles)
    {
        $this->roles[] = $roles;

        return $this;
    }

    /**
     * Remove roles
     *
     * @param \Siese\LoginBundle\Entity\Role $roles
     */
    public function removeRole(\Siese\LoginBundle\Entity\Role $roles)
    {
        $this->roles->removeElement($roles);
    }

    /**
     * Set feIngreso
     *
     * @param \DateTime $feIngreso
     * @return Usuarios
     */
    public function setFeIngreso($feIngreso)
    {
        $this->feIngreso = $feIngreso;

        return $this;
    }

    /**
     * Get feIngreso
     *
     * @return \DateTime 
     */
    public function getFeIngreso()
    {
        return $this->feIngreso;
    }

 

    /**
     * Add sedesUsuario
     *
     * @param \Siese\EntityBundle\Entity\UsuariosSedes $sedesUsuario
     * @return Usuarios
     */
    public function addSedesUsuario(\Siese\EntityBundle\Entity\UsuariosSedes $sedesUsuario)
    {
        $this->sedesUsuario[] = $sedesUsuario;

        return $this;
    }

    /**
     * Remove sedesUsuario
     *
     * @param \Siese\EntityBundle\Entity\UsuariosSedes $sedesUsuario
     */
    public function removeSedesUsuario(\Siese\EntityBundle\Entity\UsuariosSedes $sedesUsuario)
    {
        $this->sedesUsuario->removeElement($sedesUsuario);
    }

    /**
     * Get sedesUsuario
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSedesUsuario()
    {
        return $this->sedesUsuario;
    }

    /**
     * Set funcionario
     *
     * @param \Siese\EntityBundle\Entity\Funcionarios $funcionario
     * @return Usuarios
     */
    public function setFuncionario(\Siese\EntityBundle\Entity\Funcionarios $funcionario = null)
    {
        $this->funcionario = $funcionario;

        return $this;
    }

    /**
     * Get funcionario
     *
     * @return \Siese\EntityBundle\Entity\Funcionarios 
     */
    public function getFuncionario()
    {
        return $this->funcionario;
    }
    
    public function isAccountNonExpired()
    {
        return true;
    }

    public function isAccountNonLocked()
    {
        return true;
    }

    public function isCredentialsNonExpired()
    {
        return true;
    }

    public function isEnabled()
    {
        return $this->isActive;
    }    
    
    public function hasRole($role) {
      return (in_array($role, $this->getRoles()) ? true : false);
    }   
}
